package com.google.appengine.codelab;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class EstabelecimentosSuspeitosServlet extends BaseServlet 
{

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		
		//cargaTeste();  //comentado por Guilherme Vamos centralizar no massa de dados
	

		request.setAttribute("paises", Localidade.listarPaises());
		
		RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/jsp/frmEstabelecimentosSuspeitos.jsp");  
		rd.forward(request, response); 
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String datainicial = request.getParameter("dataInicial");
		String dataFinal = request.getParameter("dataFinal");
		String pais = request.getParameter("pais");
		String estado = request.getParameter("estado");
		String cidade = request.getParameter("cidade");
		
		request.setAttribute("datainicial", datainicial);
		request.setAttribute("dataFinal", dataFinal);
		request.setAttribute("usuario", "Administrador");
		request.setAttribute("estado", estado);
		request.setAttribute("cidade", cidade);
		
		try{
			Date data1 = Common.cDate(datainicial);
			Date data2 = Common.cDate(dataFinal);
			
			if( data1.after( data2 ) ){
				throw new RuntimeException("Data final maior que data inicial.");
			}
			
			Collection<ItemRelatorioEstabelecimentosSuspeitos> itens = Estabelecimento.getAllEstabelecimentosSuspeitos( 
				data1, data2,	pais, estado, cidade);

			request.setAttribute("pais", pais);
			request.setAttribute("dataAtual", new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(new Date()));
			
			request.setAttribute("itens", itens);
			
			request.getRequestDispatcher("/WEB-INF/jsp/relatorioEstabelecimentosSuspeitos.jsp").forward(request, response);  
		}
		catch(Exception e){
			request.setAttribute("erro", e.getMessage());
			request.getRequestDispatcher("/WEB-INF/jsp/frmEstabelecimentosSuspeitos.jsp").forward(request, response);
		}
	}

	public static void cargaTeste() {
		Localidade.createOrUpdateLocalidade("1", 1000F, 1000F, "localidade_address", "Jacare", "SP", "Brasil", "12309-280");
		Localidade.createOrUpdateLocalidade("2", 1000F, 1000F, "localidade_address", "Jacare", "SP", "Brasil", "12309-280");
		
		Localidade.createOrUpdateLocalidade("3", 1000F, 1000F, "localidade_address", "SJC", "SP", "Brasil", "12309-280");
		Localidade.createOrUpdateLocalidade("4", 1000F, 1000F, "localidade_address", "Mogi", "SP", "Brasil", "12309-280");
		Localidade.createOrUpdateLocalidade("5", 1000F, 1000F, "localidade_address", "Rio de Janeiro", "RJ", "Brasil", "12309-280");
		
		Estabelecimento.createOrUpdateEstabelecimento("10", "Tipo A", "Estabelecimento Teste AAAAA", "1");
		Estabelecimento.createOrUpdateEstabelecimento("20", "Tipo A", "Estabelecimento Teste BBBBB", "1");
		Estabelecimento.createOrUpdateEstabelecimento("30", "Tipo B", "Estabelecimento Teste CCCCC", "2");
		Estabelecimento.createOrUpdateEstabelecimento("40", "Tipo B", "Estabelecimento Teste DDDDD", "3");
		Estabelecimento.createOrUpdateEstabelecimento("50", "Tipo C", "Estabelecimento Teste EEEEE", "5");
		Estabelecimento.createOrUpdateEstabelecimento("60", "Tipo D", "Estabelecimento Teste FFFFF", "5");
		
		Cliente.createOrUpdateCliente("1", "cliente_name", "cliente_firstName", "cliente_lastName", "3333-3333", "cliente_cpf", "cliente_email", "1");
		Midia.createOrUpdateMidia("1", "midia_name", "midia_description", "1000", "visa", "1");
		
		Transacao.createOrUpdateTransacao("1", "type1", new BigDecimal("100.00").doubleValue(), "20130501", null, "10", "1", "1", "1");
		Transacao.createOrUpdateTransacao("2", "type1", new BigDecimal("1000.00").doubleValue(), "20130502", null, "20", "1", "1", "2");
		Transacao.createOrUpdateTransacao("3", "type1", new BigDecimal("1000.00").doubleValue(), "20130503", null, "30", "1", "1", "3");
		Transacao.createOrUpdateTransacao("4", "type1", new BigDecimal("1000.00").doubleValue(), "20130504", null, "40", "1", "1", "4");
		Transacao.createOrUpdateTransacao("5", "type1", new BigDecimal("10000.00").doubleValue(), "20130505", null, "50", "1", "1", "5");
		Transacao.createOrUpdateTransacao("6", "type1", new BigDecimal("10000.00").doubleValue(), "20130506", null, "60", "1", "1", "5");
		
		Fraude.createOrUpdateFraude("1", "fraude1", "type1", "forma_deteccao_1", "20130501", "1");
		Fraude.createOrUpdateFraude("2", "fraude2", "type1", "forma_deteccao_1", "20130502", "2");
		Fraude.createOrUpdateFraude("3", "fraude3", "type1", "forma_deteccao_1", "20130503", "3");
		Fraude.createOrUpdateFraude("4", "fraude4", "type1", "forma_deteccao_1", "20130504", "4");
		Fraude.createOrUpdateFraude("5", "fraude5", "type1", "forma_deteccao_1", "20130505", "5");
	}
	
}
